//******************************************************************************
//  _____                  _    _             __
// |  _  |                | |  | |           / _|
// | | | |_ __   ___ _ __ | |  | | __ _ _ __| |_ __ _ _ __ ___
// | | | | '_ \ / _ \ '_ \| |/\| |/ _` | '__|  _/ _` | '__/ _ \
// \ \_/ / |_) |  __/ | | \  /\  / (_| | |  | || (_| | | |  __/
//  \___/| .__/ \___|_| |_|\/  \/ \__,_|_|  |_| \__,_|_|  \___|
//       | |               We don't make the game you play.
//       |_|                 We make the game you play BETTER.
//
//            Website: http://openwarfaremod.com/
//******************************************************************************

#include openwarfare\_utils;


init()
{
	// Get the main module's dvar
	level.scr_rng_enabled = getModDvarX( "scr_rng_enabled", "int", 0, 0, 2 );

	// If R&G is disabled then there's nothing else to do here
	if ( level.scr_rng_enabled == 0 )
		return;

	// Load the rest of the module's dvars
	level.scr_rng_distance = getModDvarX( "scr_rng_distance", "int", 200, 0, 10000 );
	level.scr_rng_damage_closer = getModDvarX( "scr_rng_damage_closer", "float", 50, 0, 100 );
	level.scr_rng_damage_longer = getModDvarX( "scr_rng_damage_longer", "float", 5, 0, 100 );
	level.scr_rng_show_message = getModDvarX( "scr_rng_show_message", "int", 0, 0, 3 );
        level.scr_rng_message = getModDvarX( "scr_rng_message", "string", "" );
	level.scr_rng_shock_time = getModDvarX( "scr_rng_shock_time", "float", 2, 0, 15 );

        precacheShellShock( "frag_grenade_mp" );

	return;
}


rngDamage( eInflictor, eAttacker, iDamage, iDFlags, sMeansOfDeath, sWeapon, vPoint, vDir, sHitLoc, psOffsetTime )
{

	// Make sure we are dealing with a player that was not using the weapon sights
	if ( isDefined( eAttacker ) && isPlayer( eAttacker ) && self != eAttacker && sHitLoc != "none" && !eAttacker playerAds() ) {
		// Make sure we are dealing with a weapon that supports sights

		 if ( ( maps\mp\gametypes\_weapons::isSniperWeapon( sWeapon ) || 
                        maps\mp\gametypes\_weapons::isPistolWeapon( sWeapon ) || 
                        maps\mp\gametypes\_weapons::isSmgWeapon( sWeapon ) ||
                        maps\mp\gametypes\_weapons::isCqbWeapon( sWeapon ) ||
                        maps\mp\gametypes\_weapons::isRifleWeapon( sWeapon ) ||
                        maps\mp\gametypes\_weapons::isLmgWeapon( sWeapon ) ||
                        maps\mp\gametypes\_weapons::isShotgunWeapon( sWeapon ) ) && ( level.scr_rng_enabled == 1 || !isShotgunWeapon( sWeapon ) ) && sMeansOfDeath != "MOD_MELEE" ) {

		        // Get the distance between the player and the attacker
		        rngDistance = distance( self.origin, eAttacker.origin );

			// Check which damage percentage we need to apply
			if ( rngDistance <= level.scr_rng_distance ) {
				iDamage = int( iDamage * level.scr_rng_damage_closer / 100 );
			} else {
				iDamage = int( iDamage * level.scr_rng_damage_longer / 100 );
			}

		        if ( level.scr_rng_show_message != 0 && rngDistance >= level.scr_rng_distance ) {
			       if ( level.scr_rng_message != "" ) {
			       eAttacker iprintlnbold( level.scr_rng_message );
			       } else {
			       eAttacker iprintlnbold( &"OW_RNG_MESSAGE" );                    
		               }

                        if ( level.scr_rng_show_message == 2 && rngDistance >= level.scr_rng_distance ) eAttacker shellshock( "frag_grenade_mp", level.scr_rng_shock_time );

                        if ( level.scr_rng_show_message == 3 && rngDistance >= level.scr_rng_distance ){
                               eAttacker thread common_scripts\utility::_disableWeapon();
                               wait (0.08);
                               eAttacker shellshock( "frag_grenade_mp", level.scr_rng_shock_time );
                               wait ( level.scr_rng_shock_time );
                               eAttacker thread common_scripts\utility::_enableWeapon();
                               }
                        }

		}

	}

	return iDamage;
}

isShotgunWeapon( weapon )
{

   if ( isSubStr( weapon, "hs10_" ) )
      return true;
   if ( isSubStr( weapon, "ithaca_" ) )
      return true;
   if ( isSubStr( weapon, "rottweil72_" ) )
      return true;
   if ( isSubStr( weapon, "spas_" ) )
      return true;
   if ( isSubStr( weapon, "hs10_" ) )
      return true;
   if ( isSubStr( weapon, "ks23_" ) )
      return true;
   
   return false;
}

